package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.AtWho;
import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Controller
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService commendService;
    @Autowired
    private ITravelConcernService travelConcernService;
    @Autowired
    private ICommentService commentService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IAtWhoService atWhoService;


    //游记的评论添加
    @PostMapping("/comment")
    @ResponseBody
    public Object saveTravelComment(Comment comment){
        //保存完返回这个
        Comment commentResult = commentService.saveTravelComment(comment);
        JSONResult result = new JSONResult();
        result.setData(commentResult);
        return result;
    }


    //根据游记的ID查询用户对游记的评论
    @GetMapping("/{travelId}/comment")
    @ResponseBody
    public Object getTravelComment(CommentQueryObject qo){
        //先不设置页面大小
        qo.setPageSize(0);
        PageInfo pageInfo = commentService.getPageInfo(qo);
        return pageInfo;
    }

    //根据游记的ID查询其点赞和收藏的次数
    @GetMapping("/{travelId}/travelConcerns")
    @ResponseBody
    public Object getTravelConcerns(@PathVariable Long travelId){
        HashMap<String,Object> map = new HashMap<String, Object>();
        //游记点赞次数
        map.put("likeNums", travelConcernService.getLikeNumsByTravelId(travelId));
        //游戏的收藏次数
        map.put("collectorNums", travelConcernService.getcollectorNumsByTravelId(travelId));
        return map;
    }


    //获取游记的周推荐接口 周推荐状态为1
    @GetMapping("/commend")
    @ResponseBody
    public Object getTravelCommendByType(TravelCommendQuery qo){
        return commendService.getPageInfoApp(qo);
    }

    //index.html看所有公开并且审核的游记
    @GetMapping
    @ResponseBody
    public Object list(TravelQuery qo){
        //必须是发布状态
        qo.setState(Travel.STATE_RELEASE);
        //必须是公开状态
        qo.setIsPublic(true);
        //排序操作
        if(qo.getOrderby()==null){
            qo.setOrderby("t.releaseTime desc");
        }
        PageInfo pageInfo = travelService.getPageInfo(qo);
        return pageInfo;
    }

    @PutMapping
    @ResponseBody
    //我的游记的编辑操作
    public Object update(Travel travel) {
        System.out.println("更新操作");
        JSONResult reuslt = new JSONResult();

        try {
            travelService.update(travel);
        } catch (Exception e) {
            e.printStackTrace();
            reuslt.mark(e.getMessage());
        }
        return reuslt;
    }

    @GetMapping("/{id}")
    @ResponseBody
    //根据id查询某一篇旅游日记 回显我的游记数据
    public Object get(@PathVariable Long id) {
        JSONResult result = new JSONResult();
        //根据id获取游记
        Travel travel = travelService.get(id);
        result.setData(travel);
        return result;
    }

    @PostMapping
    @ResponseBody
    //APP记录我的旅游日记添加操作
    public Object save(Travel travel) {
        //因为使用了rest风格,因此只进行保存
        JSONResult json = new JSONResult();
        User user = UserContext.getCurrentUser();
        //获取到用户的信息
        travel.setAuthor(user);
        //测试一下文本剪切可不可以
        String inputor = travel.getTravelContent().getContent();
        try {
            Long travelId = travelService.save(travel);
            String url = "/travelContent.html?id=" + travelId;
            System.out.println(url);
            if (inputor != null) {
                String[] split = inputor.split("@");
                for (String s : split) {
                    String[] ends = s.split(" ");
                    String s1 = ends[0];
                    //需要先查询到是否存在
                    User u = userService.selectOneByNickName(s1);
                    if (u != null) {
                        AtWho atWho = new AtWho(null, user.getId(), u.getId(), user.getNickName(), u.getNickName(), url, 0);
                        atWhoService.saveAt(atWho);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
}

